﻿using Client.Model;
using Client.View.CommonView;
using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Client.ViewModel.CommonViewModel
{
    /// <summary>
    /// Author: Leil
    /// Description: OrgTreeViewModel
    /// CreateTime: 2020/6/2 14:43:01
    /// </summary>
    public class OrgTreeViewModel : BindableBase
    {
        public OrgTree OrgTreeUserControl { get; set; }

        public OrgTreeViewModel()
        {
            SelectedOrgCommand = new DelegateCommand<ViewOrg>(SelectedOrg);
            GotFocusCommand = new DelegateCommand(GotFocus);


            List<ViewOrg> list = new List<ViewOrg>();
            ViewOrg parent1 = new ViewOrg()
            {
                ID = 1,
                ParentId = 0,
                OrgName = "一级组织1"
            };
            ViewOrg parent2 = new ViewOrg()
            {
                ID = 2,
                ParentId = 0,
                OrgName = "一级组织2"
            };

            ViewOrg child1 = new ViewOrg()
            {
                ID = 3,
                ParentId = 1,
                OrgName = "二级组织1-1"
            };
            ViewOrg child2 = new ViewOrg()
            {
                ID = 4,
                ParentId = 1,
                OrgName = "二级组织1-2"
            };
            ViewOrg child3 = new ViewOrg()
            {
                ID = 5,
                ParentId = 1,
                OrgName = "二级组织1-3"
            };

            ViewOrg child4 = new ViewOrg()
            {
                ID = 6,
                ParentId = 2,
                OrgName = "二级组织2-1"
            };
            ViewOrg child5 = new ViewOrg()
            {
                ID = 7,
                ParentId = 2,
                OrgName = "二级组织2-2"
            };
            ViewOrg child6 = new ViewOrg()
            {
                ID = 8,
                ParentId = 2,
                OrgName = "二级组织2-3"
            };
            ViewOrg child7 = new ViewOrg()
            {
                ID = 9,
                ParentId = 2,
                OrgName = "二级组织2-4"
            };
            ViewOrg child8 = new ViewOrg()
            {
                ID = 10,
                ParentId = 2,
                OrgName = "二级组织2-5"
            };
            ViewOrg grandchild1 = new ViewOrg()
            {
                ID = 11,
                ParentId = 6,
                OrgName = "三级组织6-1"
            };
            list.Add(parent1);
            list.Add(parent2);
            list.Add(child1);
            list.Add(child2);
            list.Add(child3);
            list.Add(child4);
            list.Add(child5);
            list.Add(child6);
            list.Add(child7);
            list.Add(child8);
            list.Add(grandchild1);

            InitOrgTree(list);

        }

        private void InitOrgTree(List<ViewOrg> list)
        {
            Dictionary<int, ViewOrg> orgDic = list.ToDictionary(obj => obj.ID, obj => obj);
            List<ViewOrg> tree = new List<ViewOrg>();
            list.ForEach(o =>
            {
                FindAncestor(o, orgDic, tree);
            });
            OrgTree = tree;
        }

        private void FindAncestor(ViewOrg org, Dictionary<int, ViewOrg> orgDic, List<ViewOrg> tree)
        {
            if (org.ParentId == 0)
            {
                if (tree.Contains(org))
                {
                    return;
                }
                tree.Add(org);
            } else
            {
                if (orgDic.ContainsKey(org.ParentId))
                {
                    ViewOrg parent = orgDic[org.ParentId];
                    if (!parent.SubOrgs.Contains(org))
                    {
                        parent.SubOrgs.Add(org);
                    }
                    FindAncestor(parent, orgDic, tree);
                }
            }
        }


        private List<ViewOrg> orgTree;

        public List<ViewOrg> OrgTree
        {
            get { return orgTree; }
            set
            {
                orgTree = value;
                RaisePropertyChanged("OrgTree");
            }
        }


        public DelegateCommand<ViewOrg> SelectedOrgCommand { get; set; }
        private void SelectedOrg(ViewOrg org)
        {

        }


        public DelegateCommand GotFocusCommand { get; set; }
        private void GotFocus()
        {

        }

    }
}
